/*
Dominique Jannone
Guillaume Marre
Yohan Ojeda
Laurent Schiavon
*/
package model;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;


class CanalAdminHandler  extends Thread{
	
	// log
	public static final Logger logger = Logger.getLogger(CanalAdminHandler.class.getName());

	private ServerSocketChannel serverSocket;
	private final ExecutorService pool;
	private AdminServerDelegate delegate;
	private int serviceCpt = 0;
	private int port;

	CanalAdminHandler(int port, ExecutorService pool){
		logger.log(Level.INFO,"Start CanalAdminHandler on port " + port);
	    this.pool = pool;
	    this.port = port;
	    try {
		serverSocket = ServerSocketChannel.open();
		serverSocket.socket().bind(new InetSocketAddress(port));
	    }catch(IOException e){ logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e); System.exit(1);}
	}
	
	public void run(){
	    Socket sock = null;
		while (MainServer.isServerOn()) {
		    try{
			sock =  serverSocket.accept().socket();
			MainServer.logger.log(Level.INFO,"connexion accepted with " + sock.getInetAddress());
			serviceCpt++;
			//delegate = new AdminServerDelegate(sock,MainServer.this);
			//delegate.runAdmin();
		    }catch(ClosedByInterruptException e){
			interrupted(); // acquitement de l'interrupt
			logger.log(Level.INFO,"end admin");
		    }
		    catch (IOException ex) {
		    	logger.log(Level.SEVERE,ex.getClass() + ": " +  ex.getMessage(), ex); }
		}
		logger.log(Level.INFO,"end admin delegate");
	}

    }
